package com.sunday.android.fangdaifu.bukong.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.WebViewActivity;
import com.sunday.android.fangdaifu.bukong.entity.NewsImage;
import com.sunday.android.fangdaifu.bukong.entity.NewsItem;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.common.widgets.banner.holder.Holder;
import com.sunday.common.widgets.banner.holder.ViewHolderCreator;

import java.util.List;

/**
 * Created by Pual's PC on 2016/10/9.
 */
public class ViewPagerUtils {
    private Context mContext;
    private TextView textView;
    private int w;

    public ViewPagerUtils(Context mContext, ConvenientBanner viewPager, TextView textView, List<NewsItem> list, int w) {
        this.mContext = mContext;
        this.w = w;
        this.textView = textView;
        viewPager.setPages(new ViewHolderCreator<MyView>() {
            @Override
            public MyView createHolder() {
                return new MyView();
            }
        }, list);
        if (viewPager.isTurning()) {
            return;
        }
        if (w != 0) {
            viewPager.setLayoutParams(new RelativeLayout.LayoutParams(w, 7*w/16));
        }

        viewPager.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
        viewPager.setPageIndicator(new int[]{R.drawable.point_false_white, R.drawable.point_true_white});
        viewPager.setPointViewVisible(true);
        viewPager.startTurning(2500);
    }


    Transformation transformation = new Transformation() {

        @Override
        public Bitmap transform(Bitmap source) {

            int targetWidth = DeviceUtils.getDisplay(mContext).widthPixels;
            if (source.getWidth() == 0) {
                return source;
            }

            //设置的宽度比例来缩放
            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            int targetHeight = (int) (targetWidth * aspectRatio);
            if (targetHeight != 0 && targetWidth != 0) {
                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                if (result != source) {
                    // Same bitmap is returned if sizes are the same
                    source.recycle();
                }
                return result;
            } else {
                return source;
            }
        }


        @Override
        public String key() {
            return "transformation" + " desiredWidth";
        }
    };

    public class MyView implements Holder<NewsItem> {
        private ImageView imageView;

        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, final NewsItem images) {
            if (TextUtils.isEmpty(images.getImage())) {
                images.setImage("http");
            }
            if(w != 0){
                Picasso.with(imageView.getContext()).load(images.getImage()).centerCrop().resize(DeviceUtils.getDisplay(mContext).widthPixels,  7*w/16).placeholder(R.drawable.default_error).into(imageView);
            }else {
                Picasso.with(imageView.getContext()).load(images.getImage()).transform(transformation).placeholder(R.drawable.default_error).into(imageView);
            }

            if (textView != null) {
                textView.setText(images.getSummary());
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra(Constants.TITLE,Constants.NEW_TITLE);
                    intent.putExtra(Constants.URL,images.getContext());
                    mContext.startActivity(intent);
                }
            });
        }
    }
}
